package com.rj.bd.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.rj.bd.qz.SolrREUpdateJob;

import org.apache.commons.io.FileUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.rj.bd.entity.Book;
import com.rj.bd.entity.Class1;
import com.rj.bd.entity.User;
import com.rj.bd.service.IBookSerivce;
import com.rj.bd.service.IUserService;

import cn.hutool.core.util.IdUtil;

/**
 * @desc
 * @author FTY
 * @time 2021-05-31
 */ 

@RestController
@ResponseBody
@CrossOrigin
public class BookController {
	
	@Autowired
	private IBookSerivce bookSerivce;
	
	@Autowired   //注入
	public IUserService userService;
	
	/**
	 * 查询全部
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/book/queryAll")
	public Map<String,Object>query(HttpServletRequest request){
        System.out.println("BookController--->query()");
        Map<String, Object> map = new HashMap<String, Object>();
        
        List<Book>list=bookSerivce.queryAll();
        
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("data", list);
        return map;
    }
	
	/**
     * 分页&条件查询
	 * @throws IOException 
     */
    @RequestMapping(value="/book/query")//标识方法名称
    public Map<String,Object> query(@RequestParam Map maps, String name) throws SolrServerException, IOException {
        System.out.println("BookController--->query()");
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("name---"+name);

        Map<String,Object> Page=bookSerivce.getPage(maps,name);

        List<Book>list=bookSerivce.query(name);
        int count=list.size();
        System.out.println("count:"+count);

        //更新索引
//        System.out.println("更新索引");
//        SolrREUpdateJob solrREUpdateJob=new SolrREUpdateJob();
//        solrREUpdateJob.work();

        map.put("count",count);
        map.put("code", 0);
        map.put("msg", "获取信息成功");
        map.put("data", Page);
        return map;
    }
    
    
    /**
     * 删除
     */
    @RequestMapping("/book/delete")
    public  Map<String, Object> delete(@RequestParam String bid){
        System.out.println("BookController--->delete()");
        Map<String, Object> map = new HashMap<String, Object>();
        //String shangpinid = request.getParameter("shangpinid");
        //int xs_id  =Integer.parseInt(id);
        System.out.println("bid:"+bid);

        if (bid==null || bid.equals("")) {
            map.put("code", -1);
            map.put("msg", "请选择要删除的内容");
            return map;
        }

        bookSerivce.delete(bid);

        //更新索引
        SolrREUpdateJob solrREUpdateJob=new SolrREUpdateJob();
        solrREUpdateJob.work();

        map.put("code", 0);
        map.put("msg", "删除成功");
        return map;
    }
    
    
    /**
     * 添加
     */
    @RequestMapping("/book/add")
    public Map<String, Object> add(@RequestParam(value="img") MultipartFile img, Book book, HttpServletRequest request){
        System.out.println("BookController--->add()");

        Map<String, Object> map = new HashMap<String, Object>();
        String bname = request.getParameter("bname");
        String bwriter = request.getParameter("bwriter");
        String bconcern = request.getParameter("bconcern");
        String bnum = request.getParameter("bnum");
        String bprice = request.getParameter("bprice");
        //String bimg = request.getParameter("bimg");
        String cid = request.getParameter("cid");
//        String zhuangtai = request.getParameter("zhuangtai");
        String zhuangtai="1";
        
        
        System.out.println("bname:"+bname);
        System.out.println("bwriter:"+bwriter);
        System.out.println("bconcern:"+bconcern);
        System.out.println("bnum:"+bnum);
        System.out.println("bprice:"+bprice);
        System.out.println("cid:"+cid);
        System.out.println("zhuangtai:"+zhuangtai);
        System.out.println("------------>");

        if (bname==null || bname.equals("")) {
            map.put("code", -1);
            map.put("msg", "请输入书名");
            return map;
        }
        if (bwriter == null || bwriter.equals("")) {

            map.put("code", -1);
            map.put("msg", "请输入作者");
            return map;
        }
        if(bconcern==null || bconcern.equals("")){
            map.put("code", -1);
            map.put("msg", "请输入出版社");
            return map;
        }
        if (bprice==null || bprice.equals("")) {
            map.put("code", -1);
            map.put("msg", "请输入单价");
            return map;
        }
        if (cid == null || cid.equals("")) {

            map.put("code", -1);
            map.put("msg", "请选择书的种类");
            return map;
        }
        if(bnum==null || bnum.equals("")){
            map.put("code", -1);
            map.put("msg", "");
            return map;
        }

        book.setBid(IdUtil.simpleUUID());
        //book.setBid(UUID.randomUUID().toString());
        book.setBname(bname);
        book.setBwriter(bwriter);
        book.setBconcern(bconcern);
        //book.setBnum(bnum);
        book.setBnum(IdUtil.simpleUUID());
        book.setBprice(bprice);
        
        Class1 class1=new Class1();
        class1.setCid(cid);
        book.setClass1(class1);
        
        book.setBimg(book.getBimg());//图片
        
        book.setZhuangtai(zhuangtai);

        

        if (!img.isEmpty()) {
            System.out.println("进入if");
            //文件的真实名称
            String realFilename = img.getOriginalFilename();
            //获取文件格式后缀名
            String type = img.getOriginalFilename().substring(img.getOriginalFilename().indexOf("."));
            // 取当前时间戳作为文件名
            String filename = System.currentTimeMillis() + type;
            //F:\jzxy\qianduan\xuesheng
            String path =  "D://img//"+ filename;// 存放位置
            System.out.println("-------------------->"+path);


            File destFile = new File(path);
            try {

                FileUtils.copyInputStreamToFile(img.getInputStream(), destFile);// 复制临时文件到指定目录下
            } catch (IOException e) {
                e.printStackTrace();
            }

            String xs_img =filename;
            book.setBimg(xs_img);
            bookSerivce.add(book);

            //更新索引
            SolrREUpdateJob solrREUpdateJob=new SolrREUpdateJob();
            solrREUpdateJob.work();


            map.put("code", 0);
            map.put("msg", "添加成功");
            map.put("data", " ");
            return map;
        } else {
            System.out.println("未进入if");
            map.put("1", "添加失败!");
            return map;
        }
    }
    
    /**
     * @desc 查询根据id
     *
     */
    @RequestMapping("/book/queryById")
    public Map<String, Object> queryById(@RequestParam String bid){
        System.out.println("BookController--->queryById()");

        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("bid:"+bid);

        List<Map>list = bookSerivce.queryById(bid);
        
        if (list==null) {
            map.put("code", -1);
            map.put("msg","获取图书信息失败");
            return map;
        }

        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("data", list);
        return map;
    }
    
    /**
     * 修改(借书)
     */
    @RequestMapping("/book/editByZhuangTaiWei")
    public Map<String, Object> edit(@RequestParam String bid,String uid){
        System.out.println("BookController--->editByZhuangTaiWei()");

        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("bid:"+bid);
        System.out.println("uid:"+uid);
        	
        List<User> list=userService.queryByUid(uid);
        
        String zhuangtai=null;
        for (User user : list) {
			System.out.println("用户状态为:"+user.getZhuangtai());
			zhuangtai=user.getZhuangtai();
		}
        
        if(zhuangtai=="2" || zhuangtai.equals('2')){
        	map.put("code", 0);
            map.put("msg", "借书失败");
            return map;
        }
        else{
        	bookSerivce.editByZhuangTaiWei(bid);
            map.put("code", 0);
            map.put("msg", "借书成功");
            return map;
        }
    }
	

}
